//
//  QLPropertyUtils.m
//  QLBaobaoPlayerIos
//
//  Created by anan on 13-4-3.
//  Copyright (c) 2013年 baobaodev_anan. All rights reserved.
//

#import "QLPropertyUtils.h"

@implementation QLPropertyUtils

+(NSString *)getPathPlist:(NSString*)fileName;
{
    NSString *filePath;
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    if (fileName==nil) {
        filePath = [path stringByAppendingPathComponent:@"common.plist"];
    }else{
        filePath = [path stringByAppendingPathComponent:fileName];
        filePath = [filePath stringByAppendingString:@".plist"];
    }
//    NSLog(@"%@",filePath);
    return filePath;
}

+(NSString *)getPath:(NSString*)fileName;
{
    NSString *filePath;
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    if (fileName==nil) {
        filePath = [path stringByAppendingPathComponent:@"common"];
    }else{
        filePath = [path stringByAppendingPathComponent:fileName];
    }
    //QLLog(@"%@",filePath);
    return filePath;
}

/**
 *  获取字符串
 *
 *  @param key
 *  @param defaultValue
 *
 *  @return
 */
+(NSString *)getStringByKey:(NSString *)key defaultValue:(NSString *)defaultValue
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    NSString* result = [dict objectForKey:key];
    if (result==nil) {
        result = defaultValue;
    }
    return result;
}

/**
 *  获取布朗值
 *
 *  @param key
 *  @param defaultValue
 *
 *  @return
 */
+(BOOL)getBoolByKey:(NSString *)key defaultValue:(BOOL)defaultValue
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    id result = [dict objectForKey:key];
    if (result==nil) {
        return defaultValue;
    }
    return [result boolValue];
}

/**
 *  获取整形值
 *
 *  @param key
 *  @param defaultValue
 *
 *  @return
 */
+(int)getIntByKey:(NSString *)key defaultValue:(int)defaultValue
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    id result = [dict objectForKey:key];
    if (result==nil) {
        return defaultValue;
    }
    return (int)[result integerValue];
}

/**
 *  获取双精度值
 *
 *  @param key
 *  @param defaultValue
 *
 *  @return
 */
+(double)getDoubleByKey:(NSString *)key defaultValue:(double)defaultValue
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    id result = [dict objectForKey:key];
    if (result==nil) {
        return defaultValue;
    }
    return [result doubleValue];
}

/**
 *  保存字符串值
 *
 *  @param key
 *  @param value
 *
 *  @return
 */
+(BOOL)putStringByKey:(NSString *)key value:(NSString *)value
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    if (dict==nil) {
        dict = [[NSMutableDictionary alloc] init];
    }
    [dict ql_setObject:value forKey:key];
    BOOL b = [dict writeToFile:[QLPropertyUtils getPathPlist:nil] atomically:YES];
    if(!b)
    {
        return NO;
    }
    return YES;
}

/**
 *  保存布朗值
 *
 *  @param key
 *  @param value
 *
 *  @return
 */
+(BOOL)putBoolByKey:(NSString *)key value:(BOOL)value
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    if (dict==nil) {
        dict = [[NSMutableDictionary alloc] init];
    }
    [dict ql_setObject:QLBoolToNumber(value) forKey:key];
    BOOL b = [dict writeToFile:[QLPropertyUtils getPathPlist:nil] atomically:YES];
    if(!b)
    {
        return NO;
    }
    return YES;

}

/**
 *  保存整形值
 *
 *  @param key
 *  @param value
 *
 *  @return
 */
+(BOOL)putIntByKey:(NSString *)key value:(int)value
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    if (dict==nil) {
        dict = [[NSMutableDictionary alloc] init];
    }
    [dict ql_setObject:QLIntToNumber(value) forKey:key];
    BOOL b = [dict writeToFile:[QLPropertyUtils getPathPlist:nil] atomically:YES];
    if(!b)
    {
        return NO;
    }
    return YES;
}

/**
 *  保存双精度值
 *
 *  @param key
 *  @param value
 *
 *  @return
 */
+(BOOL)putDoubleByKey:(NSString *)key value:(double)value
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:nil]];
    if (dict==nil) {
        dict = [[NSMutableDictionary alloc] init];
    }
    [dict ql_setObject:QLDoubleToNumber(value) forKey:key];
    BOOL b = [dict writeToFile:[QLPropertyUtils getPathPlist:nil] atomically:YES];
    if(!b)
    {
         NSLog(@"保存失败");
        return NO;

    }
    return YES;
}

+(NSDictionary *)getDictionaryByKey:(NSString *)key
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[QLPropertyUtils getPathPlist:key]];
    return dict;
}

+(BOOL)putDictionaryByKey:(NSString *)key value:(NSDictionary *)value
{
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:value];
    
    //删除空的对
    NSArray* allKey = [dict allKeys];
    for(int i=0;i<[allKey count];i++){
        if ([dict objectForKey:[allKey objectAtIndex:i]]==nil||[dict objectForKey:[allKey objectAtIndex:i]]==[NSNull null]) {
            [dict removeObjectForKey:[allKey objectAtIndex:i]];
        }
    }
    
    BOOL success = [dict writeToFile:[QLPropertyUtils getPathPlist:key] atomically:YES];
    if (success) {
        return YES;
    }else{
        NSLog(@"保存失败");
    }
    return NO;
}

+(BOOL)deleteDictionaryByKey:(NSString *)key
{
    NSString* path = [QLPropertyUtils getPathPlist:key];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
    return YES;
}

@end
